package com.controller;

import com.bean.BootStrapTableUser;
import com.bean.Message;
import com.bean.ResultData;
import com.bean.User;
import com.mvc.ResponseBody;
import com.service.UserService;
import com.util.JSONUtil;
import com.util.TurnUtil;
import com.util.UserEnum;
import com.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author: 柏竹
 * @Description: 用户业务 Controller层
 * @Date_Created_in: 2021-05-28 21:29
 * @Modified_By: Sans
 * @Project: 快递e栈
 */
public class UserController {
    
    @ResponseBody("/user/login.do")
    public String login(HttpServletRequest req, HttpServletResponse resp){
        //接收数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //Service传参，查询存在
        boolean login = UserService.login(username , password);
        Message msg = null;
        //通过消息类进行json封装
        if (login){
            msg = new Message("登录成功",0,null);
            // 获取登录方的 登录时间 和 ip
            UserService.login_log(username);
            
            //登录后 Session记录账号
            UserUtil.setUserName(req.getSession() , username);
            if (!username.equals("root")){
                List<User> users = UserService.findUser(UserEnum.USERNAME.type , username);
                if (users != null && users.size()==1){
                    UserUtil.setUserPhone(req.getSession(), users.get(0).getUserphone());
                }
            }
        }else{
            msg = new Message("登录失败",-1,null);
        }
        //json发送
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody ("/user/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        Map<String, Integer> data = UserService.console();
        Message msg = new Message("控制台数据",0,data);
        if (data == null) {
            msg.setStatus(-1);
        }
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody ("/user/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        String offset = req.getParameter("offset");
        String pageNumber = req.getParameter("pageNumber");
        
        List<User> all = UserService.findAll(true , offset , pageNumber);
        List<BootStrapTableUser> all2 = TurnUtil.setBootStrapTableUser(all);
        
        Map<String, Integer> console = UserService.console();
        Integer size = console.get("data_Size");
        
        ResultData resultData = new ResultData(all2,size);
        return JSONUtil.toJSON(resultData);
    }
    
    @ResponseBody ("/user/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String phone = req.getParameter("userphone");
    
        List<User> user = UserService.findUser(UserEnum.USERPHONE.type , phone);
        Message msg = null;
        if (user == null) {
            msg = new Message("无此手机号",-1,null);
        }else{
            msg = new Message(0,user.get(0));
        }
        return JSONUtil.toJSON(msg);
    }
    
    
    @ResponseBody ("/user/add.do")
    public String add(HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        String userphone = req.getParameter("userphone");
        String idnumber = req.getParameter("idnumber");
        String password = req.getParameter("password");
    
        User user = new User(idnumber,username,userphone,password);
        boolean insert = UserService.insert(user);
        Message msg = null;
        if (insert){
            msg = new Message("添加成功",0,null);
        }else{
            msg = new Message("添加失败",-1,null);
        }
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody ("/user/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String userphone = req.getParameter("userphone");
        String idnumber = req.getParameter("idnumber");
        String password = req.getParameter("password");
        
        User user = new User(idnumber,username,userphone,password);
        boolean update = UserService.update(id , user);
        Message msg = null;
        if (update){
            msg = new Message("修改成功",0,null);
        }else{
            msg = new Message("修改失败",-1,null);
        }
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody ("/user/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
    
        boolean delete = UserService.delete(id);
        Message msg = null;
        if (delete){
            msg = new Message("删除成功",0,null);
        }else{
            msg = new Message("删除失败",-1,null);
        }
        return JSONUtil.toJSON(msg);
    }
    
    //账号退出（超时关闭）

}